package com.niit.spark.sql.test

import org.apache.spark.sql.{DataFrame, SparkSession}

/**
 * Date:2025/5/16
 * Author：Ys
 * Description:
 */
object SelectColumns {

  def main(args: Array[String]): Unit = {
    val spark: SparkSession = SparkSession.builder().appName("SelectColumns").master("local[*]").getOrCreate()
    spark.sparkContext.setLogLevel("ERROR")
    import spark.implicits._

    val df: DataFrame = spark.read.option("header", "true").csv("input/sql/employees.csv")

    //如果使用SQL语法 需要创建一张临时表
    df.createOrReplaceTempView("employees")
    val df1: DataFrame = spark.sql("select name,salary from employees")
    df1.show()

    //直接使用DataFrame查询
    val df2: DataFrame = df.select("name", "salary")
    df2.show()

    spark.stop()
  }

}
